﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// ECS单例记录系统
/// </summary>
/// <typeparam name="T"></typeparam>
public class EcsRecordSingletonSystem<T> : ECSRecordSystemBase where T : ECSSingletonMomentCompoentBase, new()
{
    /// <summary>
    /// 单例瞬间组件copy列表
    /// </summary>
    List<ECSSingletonMomentCompoentBase> m_RecordInfo=new List<ECSSingletonMomentCompoentBase>();
    public override void Record(int frame)
    {
        //获取片刻单例组件兵copy数据
        ECSSingletonMomentCompoentBase momCom = MyWorld.GetSingletonComp<T>().DeepCopy();
        momCom.Frame = frame;
        momCom.World = MyWorld;
        m_RecordInfo.Add(momCom);
    }
    public override void RevertToFrame(int frame)
    {
        //获取对应帧的网络同步单例组件
        T record = (T) GetSingletonRecord(frame);
        if (record !=null)
        {

            T deepCopy = (T)record.DeepCopy();
            deepCopy.World = record.World;
            //组件更改
            MyWorld.ChangeSingletonComp(deepCopy);
        }
    }
 

    public override void ClearAll()
    {
        m_RecordInfo.Clear();
    }

    public override void ClearBefore(int frame)
    {
        myFor(MyThisEnum.before,frame);
    }

    public override void ClearRecordAt(int frame)
    {
       myFor(MyThisEnum.@equals,frame);
    }
    public override void ClearAfter(int frame)
    {
        myFor(MyThisEnum.after, frame);
    }

    public override ECSSingletonMomentCompoentBase GetSingletonRecord(int frame)
    {
        for (int i = 0, j = m_RecordInfo.Count; i < j; i++)
        {
            if (m_RecordInfo[i].Frame == frame)
            {
                return m_RecordInfo[i];
            }
        }

        return null;
    }

    public override void PrintRecord(int id)
    {
        throw new NotImplementedException();
    }

    private void myFor(MyThisEnum myenum,int frame)
    {
        switch (myenum)
        {
            case MyThisEnum.before:
                for (int i = 0, j = m_RecordInfo.Count; i < j; i++)
                {
                    if (m_RecordInfo[i].Frame < frame)
                    {
                        m_RecordInfo.RemoveAt(i);
                        i--;
                        j--;
                    }
                }
                break;
            case MyThisEnum.@equals:
                for (int i = 0, j = m_RecordInfo.Count; i < j; i++)
                {
                    if (m_RecordInfo[i].Frame == frame)
                    {
                        m_RecordInfo.RemoveAt(i);
                        i--;
                        j--;
                    }
                }
                break;
            case MyThisEnum.after:
                for (int i = 0, j = m_RecordInfo.Count; i < j; i++)
                {
                    if (m_RecordInfo[i].Frame > frame)
                    {
                        m_RecordInfo.RemoveAt(i);
                        i--;
                        j--;
                    }
                }
                break;
            default:
                Debug.LogError("标志位错误");
                break;
        }
    }
    private enum MyThisEnum
    {
        before,
        equals,
        after
    }
  

    
}

